package com.pl.itsense.gwt.clientzone.ui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.pl.itsense.gwt.clientzone.client.Clientzone;
import com.pl.itsense.gwt.clientzone.model.MemberDTO;
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class RegisterPanel  extends Window{
	static RegisterPanel instance = null;
	DynamicForm form = new DynamicForm();
	TextItem login = new TextItem();  
	PasswordItem password = new PasswordItem();  
	PasswordItem repassword = new PasswordItem();
	
	
	
	TextItem firstName = new TextItem();  
	TextItem lastName = new TextItem();  
	TextItem email = new TextItem();  
	TextItem phone = new TextItem();  
	TextItem companyName = new TextItem();  
	SelectItem selectItemIntrests = new SelectItem();  

	DynamicForm form2 = new DynamicForm();

	
	CheckboxItem newsLatterItem = new CheckboxItem();
	TextAreaItem descriptionItem = new TextAreaItem();  

	MatchesFieldValidator matchesValidator = new MatchesFieldValidator();

	RegExpValidator emailValidator = new RegExpValidator();
	
	CheckboxItem regulaminItem = new CheckboxItem();
	
	
	
	
	IButton registerButton = new IButton("Utwórz konto");
	IButton backButton = new IButton("Powrót");


	
	
	public RegisterPanel() {
		instance = this;
		init();
	}

	public static void hideInstance(){
		if (instance != null){
			instance.setVisible(false);
			instance.destroy();
			instance = null;
		}
	}

	public static void showInstance(){
		if (instance != null){
			instance.setVisible(true);
		}else{
			RootPanel.get().add( new RegisterPanel());
			instance.setVisible(true);
		}
		
	}
	
	private void init(){
		setWidth(450);  
		setHeight(570);
		
		setVisible(true);
		setShowCloseButton(false);
		setTitle("Rejstracja nowego klienta - ITSense Strefa Klienta");  
		setShowMinimizeButton(false);
		setIsModal(true);  
		setShowModalMask(true);  
		centerInPage();  
		setCanDragReposition(false);

		firstName.setName("firstName");
		firstName.setTitle("Imię");
		firstName.setRequired(false);
		firstName.setWidth(200);

		lastName.setName("lastName");
		lastName.setTitle("Nazwisko");
		lastName.setRequired(false);
		lastName.setWidth(200);

		
		login.setName("login");
		login.setTitle("Login");
		login.setRequired(true);
		login.setWidth(200);
		
		email.setName("email");
		email.setTitle("Email");  
		email.setRequired(true);
		email.setWidth(200);

		  
		emailValidator.setErrorMessage("Podano nieprawidłowy addres Email");  
		emailValidator.setExpression("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");  
		email.setValidators(emailValidator);  
		
		
		
		password.setName("password");
		password.setTitle("Hasło");  
		password.setRequired(true);
		password.setWidth(200);

		
		repassword.setName("repassword");
		repassword.setTitle("Powtórz hasło");  
		repassword.setRequired(true);  
		repassword.setWrapTitle(false);
		repassword.setWidth(200);

		phone.setName("phone");
		phone.setTitle("Telefon");  
		phone.setRequired(false);
		phone.setWidth(200);

		companyName.setName("companyName");
		companyName.setTitle("Firma");  
		companyName.setRequired(false);
		companyName.setWidth(200);

		
		
		
		descriptionItem.setName("descriptionItem");  
		descriptionItem.setTitle("Komentarz");  
		descriptionItem.setWidth(250);
		
		  
		matchesValidator.setOtherField("password");  
		matchesValidator.setErrorMessage("Podano różne hasła");          
		repassword.setValidators(matchesValidator);  
		
		
		  
		regulaminItem.setName("regulaminTerms");  
		regulaminItem.setTitle("Wyrażam zgodę na przetwarzanie moich  danych osobowych <br/> przez firmę ITSense Honorata Bojar. Dane osobowe przetwarzane <br/> będą przy  zachowaniu zasad określonych w Ustawie o ochronie <br/> danych  osobowych (Dz. U. z 1997 r. Nr 133. poz. 883 z póżniejszymi <br/> zmianami). Każda osoba ma prawo wglądu do swoich danych, ich <br/> poprawiania i usunięcia.");  
		regulaminItem.setRequired(true);  
		regulaminItem.setWrapTitle(true);
		regulaminItem.setRequiredMessage("Musisz zaakceptować warunki korzystania z serwisu");
		regulaminItem.setWidth(250);
		
		newsLatterItem.setName("newsLatterTerms");  
		newsLatterItem.setTitle("Tak, chcę otrzymywać newsletter z informacjami o systemach, <br/> usługach i nowościach w firmie ITSense");  
		newsLatterItem.setWrapTitle(true);
		newsLatterItem.setRequired(false);
		
		selectItemIntrests.setName("intrests");
		selectItemIntrests.setTitle("Zainteresowania");  
		selectItemIntrests.setMultiple(true);
		selectItemIntrests.setWidth(250);
		selectItemIntrests.setMultipleAppearance(MultipleAppearance.PICKLIST);  
		selectItemIntrests.setValueMap("Bazy danych", "Hurtownie danych","Systemy raportowe","Systemy analityczne","Business intelligence","Intranet","Obsluga informatyczne","Modelowanie procesów biznesowych"); 
 
		
		
		form.setWidth(1);
		form.setFields(login, password, repassword, firstName, lastName , email, companyName, phone, selectItemIntrests, descriptionItem);
		
		
		form2.setFields( newsLatterItem, regulaminItem);

		
		form.setTop(20);
		form.setLeft(40);
		
		form2.setTop(360);
		form2.setLeft(20);
		
		
		backButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Clientzone.setApplicationState(0);
			}
		});
		
		
		registerButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {

				if ( form.validate() && form2.validate()){
					MemberDTO memberDTO = new MemberDTO();
					memberDTO.setLogin(form.getValueAsString("login"));
					memberDTO.setPassword(form.getValueAsString("password"));
					memberDTO.setFirstName(form.getValueAsString("firstName"));
					memberDTO.setCompany(form.getValueAsString("companyName"));
					memberDTO.setLastName(form.getValueAsString("lastName"));
					memberDTO.setEmail(form.getValueAsString("email"));
					memberDTO.setNewsletter((Boolean)form2.getValue("newsLatterTerms"));
					memberDTO.setComment(form.getValueAsString("descriptionItem"));
					memberDTO.setPhone(form.getValueAsString("phone"));
					memberDTO.setIntrests(form.getValueAsString("intrests"));

					SC.showPrompt("Proszę czekać ...");
					Clientzone.clientZoneService.register(memberDTO, new AsyncCallback<MemberDTO>() {
						
						@Override
						public void onSuccess(MemberDTO result) {
							SC.clearPrompt();
							if (result.getId() > 0){
								Clientzone.memberDTO = result;
								SC.say("Konto zostało utworzone. Na wskazany adres Email została wsyłana instrukacja aktywacji konta.", new BooleanCallback() {
									@Override
									public void execute(Boolean value) {
										Clientzone.setApplicationState(0);
									}
								});
								
							}else{
								SC.warn("Użytkownik o podanej nazwie już istnieje w naszym serwisie. Spróbuj podać inny Login.");
							}
						}
						
						@Override
						public void onFailure(Throwable caught) {
							SC.clearPrompt();
						}
					});
				}
				
			}
		});
		
		
		
		VLayout vLayout = new VLayout();
		vLayout.addChild(form);
		vLayout.addChild(form2);
		
		HLayout hLayout = new HLayout();
		hLayout.setHeight(30);
		hLayout.addMember(registerButton);
		hLayout.addMember(backButton);		
		hLayout.setTop(500);
		hLayout.setLeft(125);
		hLayout.setMembersMargin(5);  

		vLayout.addChild(hLayout);
		addItem(vLayout);

	}
	

}
